//-----------------------------------------------------------------------
// <copyright file="CompanyAndCertificateDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class CompanyAndCertificateDAOBase
    {
        #region Common methods
        public virtual CompanyAndCertificate CreateCompanyAndCertificateFromReader(IDataReader reader)
        {
            CompanyAndCertificate item = new CompanyAndCertificate();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("ComAndCerID"))) item.ComAndCerID = (int)reader["ComAndCerID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CompanyID"))) item.CompanyID = (long)reader["CompanyID"];
                if (!reader.IsDBNull(reader.GetOrdinal("CertificateID"))) item.CertificateID = (int)reader["CertificateID"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndCertificateFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateCompanyAndCertificate methods
            
        public virtual void CreateCompanyAndCertificate(CompanyAndCertificate companyAndCertificate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndCertificateInsert");
                
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndCertificate.CompanyID);
                database.AddInParameter(dbCommand, "@CertificateID", DbType.Int32, companyAndCertificate.CertificateID);
                database.AddOutParameter(dbCommand, "@ComAndCerID", DbType.Int32, 4);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                companyAndCertificate.ComAndCerID = (int)database.GetParameterValue(dbCommand, "@ComAndCerID");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateCompanyAndCertificateException, ex);
            }
        }

        #endregion

        #region UpdateCompanyAndCertificate methods
        
        public virtual void UpdateCompanyAndCertificate(CompanyAndCertificate companyAndCertificate)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndCertificateUpdate");            
                
                database.AddInParameter(dbCommand, "@ComAndCerID", DbType.Int32, companyAndCertificate.ComAndCerID);
                database.AddInParameter(dbCommand, "@CompanyID", DbType.Int64, companyAndCertificate.CompanyID);
                database.AddInParameter(dbCommand, "@CertificateID", DbType.Int32, companyAndCertificate.CertificateID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateCompanyAndCertificateException, ex);
            }
        }
        
        #endregion

        #region DeleteCompanyAndCertificate methods
        public virtual void DeleteCompanyAndCertificate(int comAndCerID)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndCertificateDelete");
                
                database.AddInParameter(dbCommand, "@ComAndCerID", DbType.Int32, comAndCerID);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteCompanyAndCertificateException, ex);
            }
        }

        #endregion

        #region GetCompanyAndCertificate methods
        
        public virtual CompanyAndCertificate GetCompanyAndCertificate(int comAndCerID)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndCertificateGet");
                
                database.AddInParameter(dbCommand, "@ComAndCerID", DbType.Int32, comAndCerID);
                
                CompanyAndCertificate companyAndCertificate =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        companyAndCertificate = CreateCompanyAndCertificateFromReader(reader);
                        reader.Close();
                    }
                }
                return companyAndCertificate;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndCertificateException, ex);
            }
        }
        
        #endregion

        #region GetCompanyAndCertificateList methods
        public virtual CompanyAndCertificateCollection GetCompanyAndCertificateList(CompanyAndCertificateColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndCertificateGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndCertificateCollection companyAndCertificateCollection = new CompanyAndCertificateCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndCertificate companyAndCertificate = CreateCompanyAndCertificateFromReader(reader);
                        companyAndCertificateCollection.Add(companyAndCertificate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndCertificateCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndCertificateListException, ex);
            }
        }
        
        public virtual CompanyAndCertificateCollection GetCompanyAndCertificateList(CompanyAndCertificateColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetCompanyAndCertificateList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetCompanyAndCertificateList methods
        public virtual CompanyAndCertificateCollection GetCompanyAndCertificateListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spCompanyAndCertificateGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                CompanyAndCertificateCollection companyAndCertificateCollection = new CompanyAndCertificateCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        CompanyAndCertificate companyAndCertificate = CreateCompanyAndCertificateFromReader(reader);
                        companyAndCertificateCollection.Add(companyAndCertificate);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return companyAndCertificateCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetCompanyAndCertificateListException, ex);
            }
        }
        
        public virtual CompanyAndCertificateCollection GetCompanyAndCertificateListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetCompanyAndCertificateListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}